﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Tssipl.PaymentAdapter.Core;
using Tssipl.PaymentAdapter.MessageFormat.Sfms;
namespace Tssipl.PaymentAdapter.MessageEntity.Sfms.Neft
{
    public class NeftOutwardInwardCreditConfirmationGroupItem
    {
        public string TRN { get; set; }//16x //M
        public string IFSCOfOriginatorOfRemittance { get; set; }//4!a4!c[3!c]         //M 
        public string RelatedReference { get; set; }//16x //M        
        public string AmtCreditedTime   { get; set; }//2!c //M
        
    }
    public class NeftOutwardInwardCreditConfirmation : Tssipl.PaymentAdapter.MessageEntity.Sfms.SfmsMessageEntityBase
    {
        public SfmsBlock4 block4 { get; set; }

        public string TRN { get; set; }//16x //M
       
        public List<NeftOutwardInwardCreditConfirmationGroupItem> GroupItemList { get; set; }

        public NeftOutwardInwardCreditConfirmation(MessageParseResults results)
            : base(results)
        {
            SfmsMessage message = (SfmsMessage)results.getMessage();
            SfmsBlock4 block4 = message.getBlock4();
            this.TRN = block4.getField("2020").Trim();

            List<Field> block4FieldList = block4.getFields();
            //foreach (SfmsField field in block4.getFields())
            this.GroupItemList = new List<NeftOutwardInwardCreditConfirmationGroupItem>();
            NeftOutwardInwardCreditConfirmationGroupItem groupItem = null;
            for (int i = 4; i < block4FieldList.Count; i++)
            {
                if (block4FieldList[i].getTag() == "2020")
                {
                    if (groupItem != null)
                    {
                        this.GroupItemList.Add(groupItem);
                    }
                    groupItem = new NeftOutwardInwardCreditConfirmationGroupItem();
                    groupItem.TRN = block4FieldList[i].getValue();                    
                }
                if (block4FieldList[i].getTag() == "5518") groupItem.IFSCOfOriginatorOfRemittance= block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "2006") groupItem.RelatedReference = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "3501") groupItem.AmtCreditedTime = block4FieldList[i].getValue();                

            }
            if (groupItem != null)
            {
                this.GroupItemList.Add(groupItem);
            }
        }
        new public string Build()
        {
            block4 = new SfmsBlock4();

            // this.TRN = block4.getField("2020").Trim();
            block4.Fields.Add(new Field("2020", this.TRN));

            foreach (NeftOutwardInwardCreditConfirmationGroupItem groupItem in this.GroupItemList)
            {
                // NeftCreditTransactionGroupItem groupItem = this.GroupItemList[0];

                if (string.IsNullOrEmpty(groupItem.TRN) == false) block4.Fields.Add(new Field("2020", groupItem.TRN));
                //if (block4FieldList[i].getTag() == "5518") groupItem.IFSCOfOriginatorOfRemittance= block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.IFSCOfOriginatorOfRemittance) == false) block4.Fields.Add(new Field("5518", groupItem.IFSCOfOriginatorOfRemittance));
                //if (block4FieldList[i].getTag() == "2006") groupItem.RelatedReferenceNumber = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.RelatedReference) == false) block4.Fields.Add(new Field("2006", groupItem.RelatedReference));
                // if (block4FieldList[i].getTag() == "3501") groupItem.AmtCreditedTime = block4FieldList[i].getValue();           
                if (string.IsNullOrEmpty(groupItem.AmtCreditedTime) == false) block4.Fields.Add(new Field("3501", groupItem.AmtCreditedTime));                 
            }

            return base.Build() + block4.getFormattedBlock();
        }
    }
}
